/**
 * <strong>Title : StandardCapacity.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.cm.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2016年1月13日 上午11:50:59<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 *          <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 *          <br>
 *          <br>
 */
package cn.b.sky.mes.cm.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.StandardCapacity;
import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.model.condition.StandardCapacityCondition;
import cn.b.sky.mes.cm.service.StandardCapacityService;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : StandardCapacity.java<br></strong>
 * <strong>Package : cn.b.sky.mes.cm.controller<br></strong>
 * <strong>Description : </strong>标准产能控制器<br> 
 * <strong>Create on : 2016年1月13日 上午11:50:59<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("standardCapacity")
public class StandardCapacityController extends SecurityController {

  @Resource
  StandardCapacityService standardCapacityService;

  @Resource
  WorkCenterService workCenterService;

  @RequestMapping("standardCapacity_list")
  public String selectStandardCapacityListByCondition(
      @ModelAttribute("condition") StandardCapacityCondition standardCapacityCondition, Model model, String navTabId) {
    standardCapacityCondition.setSiteCode(getSiteCode());
    List<StandardCapacity> list = standardCapacityService
        .selectStandardCapacityListByCondition(standardCapacityCondition);
    int count = standardCapacityService.selectStandardCapacityCountByCondition(standardCapacityCondition);
    standardCapacityCondition.setData(list);
    standardCapacityCondition.setTotalCount(count);
    model.addAttribute("page", standardCapacityCondition);
    model.addAttribute(NAVTABID, navTabId);
    return "cm/standardCapacity/standardCapacity_list";
  }

  @RequestMapping("addStandardCapacity")
  public String addStandardCapacity(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "cm/standardCapacity/standardCapacity_add";
  }

  @RequestMapping("editStandardCapacity")
  public String editStandardCapacity(Model model, String navTabId, String id) {
    String view = "cm/standardCapacity/standardCapacity_add";
    String[] strArr = id.split("-");
    if (strArr.length < 2) {
      return view;
    }
    String workCenterCode = strArr[0];
    String materialCode = strArr[1];
    StandardCapacity standardCapacity = standardCapacityService.getStandardCapacity(workCenterCode, materialCode,
        getSiteCode());

    model.addAttribute("standardCapacity", standardCapacity);
    model.addAttribute(NAVTABID, navTabId);
    return view;
  }

  @RequestMapping("viewStandardCapacity")
  public String viewStandardCapacity(Model model, String navTabId, String id) {
    String view = "cm/standardCapacity/standardCapacity_view";
    String[] strArr = id.split("-");
    if (strArr.length < 2) {
      return view;
    }
    String workCenterCode = strArr[0];
    String materialCode = strArr[1];
    StandardCapacity standardCapacity = standardCapacityService.getStandardCapacity(workCenterCode, materialCode,
        getSiteCode());
    model.addAttribute("standardCapacity", standardCapacity);
    model.addAttribute(NAVTABID, navTabId);
    return view;
  }

  @RequestMapping(value = "saveStandardCapacity")
  @ResponseBody
  public DWZResult saveStandardCapacity(Model model, StandardCapacity standardCapacity, String navTabId, String id) {
    if (standardCapacity == null) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    standardCapacity.setSiteCode(getSiteCode());
    setCreateInfo(standardCapacity);
    if (StringUtils.isBlank(id)) {
      StandardCapacity sc = standardCapacityService.getStandardCapacity(standardCapacity.getWorkCenterCode(),
          standardCapacity.getMaterialCode(), getSiteCode());
      //已存在校验
      if (sc != null && !StringUtils.isBlank(sc.getMaterialCode())) {
        return ajaxError(getImeViewMessageI18n(null, "workCenterAlreadyStandardCapacity.M"));
      }
      standardCapacityService.insertStandardCapacity(standardCapacity);
    } else {
      standardCapacityService.updateStandardCapacity(standardCapacity);
    }
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
  }

  @RequestMapping("deleteStandardCapacity")
  @ResponseBody
  public DWZResult deleteMaterialType(Model model, String navTabId, HttpServletRequest request) {

    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] codes = ids.split(",");
    int len = codes.length;
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      String[] strArr = codes[i].split("-");
      reports[i] = standardCapacityService.deleteStandardCapacity(strArr[0], strArr[1], getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("selectUnlockedWorkCenter")
  @ResponseBody
  public List<WorkCenter> selectUnlockedWorkCenter(String workCenterCode) {
    WorkCenter workCenter = new WorkCenter();
    workCenter.setWorkCenterCode(workCenterCode);
    return workCenterService.getWorkCenterListByCondition(workCenter);
  }

  @RequestMapping("isExsitsStandardCapacity")
  @ResponseBody
  public boolean isExsitsStandardCapacity(String workCenterCode, String materialCode) {

    StandardCapacity standardCapacity = standardCapacityService.isExsitsStandardCapacity(workCenterCode, materialCode,
        getSiteCode());
    if (standardCapacity != null && StringUtils.isNotBlank(standardCapacity.getWorkCenterCode())) {
      return false;
    }
    return true;
  }

}
